package com.zhiyou.app.shop.website.util;

import java.sql.Timestamp;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.zhiyou.app.shop.common.util.ShopConstants;
import com.zhiyou.app.shop.website.dao.pojo.DicItem;
import com.zhiyou.app.shop.website.dao.pojo.EpList;
import com.zhiyou.app.shop.website.dao.pojo.User;
import com.zhiyou.app.shop.website.dao.pojo.UserAccount;
import com.zhiyou.app.shop.website.service.IDicItemService;
import com.zhiyou.app.shop.website.service.IEpListService;
import com.zhiyou.app.shop.website.service.IUserAccountService;
import com.zhiyou.app.shop.website.service.IUserInfoService;


public class EpTaskJob {

	private static Log log = LogFactory.getLog(EpTaskJob.class);
	
	public void monitorEpTime(){
		try {
			log.info("处理任务开始...........");
			//业务逻辑代码调用
			IEpListService epListService = (IEpListService) SpringBeanFactory.getBean("epListService");
			IDicItemService dicItemService = (IDicItemService) SpringBeanFactory.getBean("dicItemService");
			IUserInfoService userInfoService = (IUserInfoService)SpringBeanFactory.getBean("userInfoService");
			IUserAccountService userAccountService = (IUserAccountService)SpringBeanFactory.getBean("userAccountService");
			Integer deadtime = 0;
			List<DicItem> dicItems = dicItemService.findByType(ShopConstants.EP_TIME);
			if(dicItems != null && dicItems.size() > 0){
				deadtime = new Integer(dicItems.get(0).getCode());
			}
			List<EpList> list = epListService.findEpTrading();
			Long newTime = System.currentTimeMillis();
			if(list != null && list.size() > 0){
				for (EpList epList : list) {
					if(epList.getDealDate() != null && !"".equals(epList.getDealDate())&& "2".equals(epList.getStatus())){ //交易中的ep
						Long eptime = epList.getDealDate().getTime()+((deadtime*60*60*1000));
						if(eptime < newTime){ //超时   解冻
							//取的卖方的账户  将奖金币加到卖方中 删除这条ep交易信息  
							if("1".equals(epList.getType())&&"2".equals(epList.getStatus())){ //买方没有汇钱
								User buy = userInfoService.findByAccount(epList.getRelationId());
								User sale = userInfoService.findByAccount(epList.getUserId());
								UserAccount account = userAccountService.findByUserAccount(sale.getUserAccount());
								account.setBonusMoney(account.getBonusMoney().add(epList.getDealAmount()));
								if(Integer.parseInt(buy.getCreditLevel())>0){//扣除买方信用度
									buy.setCreditLevel((Integer.parseInt(buy.getCreditLevel())-1)+"");									
								}
								userAccountService.saveAccount(account);
								userInfoService.save(buy);
								epListService.delete(epList);
							}else if("2".equals(epList.getType())&&"2".equals(epList.getStatus())){//卖方没有确认
								User buy = userInfoService.findByAccount(epList.getRelationId());
								User sale = userInfoService.findByAccount(epList.getUserId());
								if(Integer.parseInt(sale.getCreditLevel())>0){//扣除卖方信用度
									sale.setCreditLevel((Integer.parseInt(sale.getCreditLevel())-1)+"");									
								}
								userInfoService.save(sale);
								
							}
						}
					}
				}
			}
			
		} catch (Exception e) {
			log.error("处理任务出现异常",e);
		}
	}
	
}
